package com.rightsidetech.scheduler.task;

import com.rightsidetech.activity.entity.ActCouponActivity;
import com.rightsidetech.activity.entity.ActCouponActivityStatis;
import com.rightsidetech.activity.service.ActCouponService;
import com.rightsidetech.trade.dtos.response.TdCouponSumAmountStatis;
import com.rightsidetech.trade.service.TdStatisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@EnableScheduling
public class ActCouponTask implements SchedulingConfigurer {

    @Resource
    private ActCouponService actCouponService;
    @Resource
    private TdStatisService tdStatisService;

    public void countTaskStatis(){
        //没有则去查询活动结束,并且没有被统计过的优惠券活动
        List<Long> activityList = activityList = actCouponService.findStatisActivityId();

        if(activityList.isEmpty()){
//            return ResponseData.fail("没有找到可以统计的优惠活动");
            return ;
        }

        for(Long activityId:activityList){
            ActCouponActivityStatis statis = new ActCouponActivityStatis();
            //统计优惠券活动
            ActCouponActivity activity2 = actCouponService.getActivityByActivityId(activityId);
            //查找已被使用的优惠券
            Long getCountNum = actCouponService.findCountByActivityId(activityId,(byte) 0);
            //已使用的优惠券
            Long usedCountNum = actCouponService.findCountByActivityId(activityId,(byte) 1);
            //统计金额
            TdCouponSumAmountStatis amountStatis = tdStatisService.statisTotalAmountByActivityId(activityId);

            statis.setReceiveNum(getCountNum + usedCountNum);
            statis.setUseNum(usedCountNum);
            statis.setCouponActivityId(activityId);
            statis.setQuantity(activity2.getTotalQuantity());
            statis.setStatisAt(new Date());
            statis.setTotalPayableAmount(amountStatis.getTotalAmount());
            statis.setTotalPayAmount(amountStatis.getActualAmount());
            statis.setTotalDiscountAmount(amountStatis.getDiscountAmount());

            actCouponService.saveActivityStatis(statis);
        }
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                new Runnable() {
                    @Override
                    public void run() {
                        countTaskStatis();
                    }
                }, new Trigger() {
                    @Override
                    public Date nextExecutionTime(TriggerContext triggerContext) {
                        //查询任务的执行周期corn表达式.
                        String cornExpression = "0 59 23 * * ?";

                        if (null != cornExpression) {
                            CronTrigger trigger = new CronTrigger(cornExpression);
                            Date nextExec = trigger.nextExecutionTime(triggerContext);
                            return nextExec;
                        }
                        return null;
                    }
                });
    }
}
